<template>
	<view class="content">
		<view class="app-title ac">
			<view class="fl" @click="back">
				<image class="back" src="/static/img/back.png"></image>
			</view>
			设备详情
		</view>
		<view class="line-select">
			<view class="tagClass1">
				<view class="tagClass_item" v-for="(item) in tagList.slice(0, 3)" :key="item.id"
					@click="clickTag(item)">
					<view v-if="item.name == '全部' || (item.name != '全部' && item.count)">
						<u-tag :mode="item.id == activeTag ? 'dark' : 'light'" :type="item.type ? item.type : 'info'"
							v-if="item.count" :text="item.name + '(' + item.count + ')  '"></u-tag>
						<u-tag :mode="item.id == activeTag ? 'dark' : 'light'" :type="item.type ? item.type : 'info'"
							v-else :text="item.name"></u-tag>
					</view>
				</view>
			</view>
			<view class="tagClass">
				<view class="tagClass_item" v-for="(item) in tagList.slice(3, tagList.length)" @click="clickTag(item)"
					:key="item.id">
					<view>
						<u-tag :mode="item.id == activeTag ? 'dark' : 'light'" :type="item.type ? item.type : 'info'"
							v-if="item.count" :text="item.name + '(' + item.count + ')  '"></u-tag>
						<u-tag :mode="item.id == activeTag ? 'dark' : 'light'" :type="item.type ? item.type : 'info'"
							v-else :text="item.name"></u-tag>
					</view>
				</view>
			</view>
			<scroll-view scroll-y="true" class="scroll-Y">
				<u-card padding="20" margin="5px 10px" :show-head="false" v-for="(deviceItem, index2) in copyTaskList"
					:key="deviceItem.equId">
					<view class="body_box" slot="body">
						<view class="body_box_item">
							<u-cell-item title="设备名称" :value="deviceItem.equName" :arrow="false"></u-cell-item>
							<!-- 监督功能 -->
							<u-cell-item v-if="columShow.monitor" title="监督状态" :arrow="false">
								<view slot="right-icon" class="tools">
									<u-icon v-if="!deviceItem.monitorStatus" name="clock" color="#c8c9cc"></u-icon>
									<u-icon v-if="deviceItem.monitorStatus == 1" name="checkbox-mark"
										color="#19be6b"></u-icon>
									<u-icon v-if="deviceItem.monitorStatus == 2" name="close"
										@click="reviewInfo(deviceItem, 1)" color="#fa3534"></u-icon>
								</view>
							</u-cell-item>
							<!-- 卡控功能 -->
							<u-cell-item v-if="columShow.monitor" title="卡控状态" :arrow="false">
								<view slot="right-icon" class="tools">
									<u-icon v-if="deviceItem.controlStatus == null" name="clock"
										color="#c8c9cc"></u-icon>
									<u-icon v-if="deviceItem.controlStatus == 0" name="checkbox-mark"
										color="#19be6b"></u-icon>
									<u-icon v-if="deviceItem.controlStatus == 1" @click="reviewInfo(deviceItem, 2)"
										name="close" color="#fa3534"></u-icon>
								</view>
							</u-cell-item>
							<!--不是监督卡控的设备 标记是否检修 -->
							<view v-if="commonEqui()" @click="taskIndex = index2">
								<u-cell-item title="检修完成" :arrow="false" arrow-direction="down">
									<view slot="right-icon" class="tools">
										<u-checkbox-group v-if="
                        config.woState == '进行中' ||
                        deviceItem.equStatus === '1'
                      ">
											<!-- :disabled="config.woState!='进行中'|| deviceItem.equStatus=='1'" -->
											<u-checkbox @change="changeCheckbox($event, index2)" acrive-color="#19be6b"
												:disabled="deviceItem.equStatus === '1'"
												v-model="deviceItem.equStatus ? true : false"></u-checkbox>
											<!-- -->
										</u-checkbox-group>
									</view>
								</u-cell-item>
								<u-cell-item title="开始时间:" :value="
                    deviceItem.startTime == '' ? '请选择' : deviceItem.startTime
                  " @click="deviceShow() ? checkStartTime(index2) : ''">
								</u-cell-item>
								<u-cell-item title="结束时间:" :value="
                    deviceItem.endTime == '' ? '请选择' : deviceItem.endTime
                  " @click="
                    deviceShow() && deviceItem.startTime
                      ? checkEndTime(index2)
                      : ''
                  ">
								</u-cell-item>
							</view>
							<view class="rowClass" v-if="columShow.scanCode || columShow.nfc">
								<u-cell-item v-if="columShow.scanCode" title="设备扫码" :arrow="false" arrow-direction="">
									<view slot="right-icon" class="tools">
										<u-button v-if="deviceItem.nfcStatus != 1 && deviceShow()" type="primary"
											size="mini" :ripple="true" @click="scanCode(index2)">扫码</u-button>
										<text v-else>{{ deviceShow() ? "成功" : "" }}</text>
									</view>
								</u-cell-item>
								<u-cell-item v-if="columShow.nfc" title="NFC读取" :arrow="false" arrow-direction="">
									<view slot="right-icon" class="tools">
										<u-button v-if="deviceItem.nfcStatus != 2 && deviceShow()" type="primary"
											size="mini" :ripple="true" @click="testNFC(index2)">NFC</u-button>
										<text v-else>{{ deviceShow() ? "识别成功" : "" }}</text>
									</view>
								</u-cell-item>
							</view>
							<u-cell-item v-if="
                  columShow.monitorButton || columShow.scanCode || columShow.nfc
                " title="开始时间" :arrow="false" arrow-direction="down">
								<view slot="right-icon" class="tools">
									<!-- <u-button v-if="!deviceItem.startTime && deviceShow()" :plain="false" type="primary"
										size="mini" :ripple="true"
										@click="!deviceItem.startTime ? start(index2) : ''">开始检修</u-button>
									<text v-else>{{ deviceItem.startTime }}</text> -->
									<text>{{ deviceItem.startTime }}</text>
								</view>
							</u-cell-item>
							<u-cell-item v-if="
                  columShow.monitorButton || columShow.scanCode || columShow.nfc
                " title="结束时间" :arrow="false" arrow-direction="down">
								<view slot="right-icon" class="tools columsItem">
									<u-button v-if="!deviceItem.endTime && deviceShow()" :disabled="
                      !deviceItem.startTime ||
                      (columShow.scanCode &&
                        deviceItem.nfcStatus != 1 &&
                        deviceItem.nfcStatus != 2) ||
                      (columShow.nfc &&
                        deviceItem.nfcStatus != 2 &&
                        deviceItem.nfcStatus != 1)
                    " :plain="false" type="primary" size="mini" :ripple="true"
										@click="!deviceItem.endTime ? end(index2) : ''">结束检修</u-button>
									<text v-else>{{ deviceItem.endTime }}</text>
									<u-button class="recheck" v-if="
                      deviceItem.endTime &&
                      deviceShow() &&
                      deviceItem.monitorStatus == 2
                    " :disabled="!deviceItem.startTime" :plain="false" type="primary" size="mini" :ripple="true"
										@click="recheck(index2)">复检</u-button>
								</view>
							</u-cell-item>
							<view class="onlineMonit" v-if="btn == 2">
								<u-button :plain="true" type="primary" :ripple="true"
									@click="onlineMonit(deviceItem)">在线监测</u-button>
							</view>
							<u-line color="#ecf5ff" />
						</view>
					</view>
				</u-card>
			</scroll-view>
		</view>
		<u-picker v-model="show_date" mode="time" :params="params" @confirm="setDate" @cancel="onCancel"></u-picker>
		<u-modal v-model="showInfo" width="auto" :title-style="titleStyle" :content-style="modelContent"
			:show-confirm-button="false" :show-cancel-button="true" :title="title" :show-title="true"
			:mask-close-able="true">
			<view class="slot-content">
				<rich-text v-if="description" :nodes="description"></rich-text>
				<u-empty v-else></u-empty>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import selectDevice from "../../components/myWork/viewWork/selectDevice.vue";
	import {
		WORK_CONTROLLER
	} from "../../api/pms";
	var NfcAdapter;
	import NFC from "../../utils/nfc.js";
	import {
		initSpeech
	} from "../../utils/ttsSpeech.js";
	import {
		CJX_DATA_CONTROLLER
	} from "../../api/t5";
	export default {
		props: {
			detailContent: {},
		},
		components: {
			selectDevice,
		},
		data() {
			return {
				// overInfo: {},
				taskList: [],
				pmsWoDetailed: [],
				PmsWoinfor: {},
				copyTaskList: [],
				show_picker: false,
				taskIndex: "",
				disabled: false,
				show: false,
				showInfo: false,
				description: "",
				modelContent: {
					padding: "15px",
				},
				titleStyle: {
					paddingTop: "16px",
					fontWeight: 600,
				},
				title: "",
				columShow: {
					monitor: true,
					monitorButton: false,
					control: true,
					startTime: true,
					endTime: true,
					scanCode: false,
					nfc: false,
				},
				btn: "",
				woSn: "",
				activeTag: "",
				tagList: [{
						name: "全部",
						count: 0,
						type: "primary",
						id: "all",
					},
					{
						name: "监督",
						count: 0,
						type: "warning",
						id: "view",
					},
					{
						name: "卡控",
						count: 0,
						type: "error",
						id: "control",
					},
				],
				show_date: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true,
				},
				date_type: "",
				config: {
					pmsEquControl: "",
					pmsNfcShow: "",
					pmsScanCode: "",
					woState: "",
				},
				clickTagParam: {
					name: "全部",
					count: 0,
					type: "primary",
					id: "all",
				},
				speechConfig: {
					speech: 30,
					pitch: 50,
				},
				speechSwitch: true, // 语音播报功能开关
			};
		},
		async onLoad(options) {
			// console.log(getApp().globalData)
			this.btn = options.btn;
			this.type = options.type;
			this.config = JSON.parse(options.config);
			// 控制某些列是否显示
			this.columShow.monitor = this.config.pmsEquControl == 1;
			this.columShow.monitorButton = this.config.pmsEquControl == 1;
			this.columShow.nfc = this.config.pmsNfcShow == 1;
			this.columShow.scanCode = this.config.pmsScanCode == 1;
		},

		mounted() {
			this.initDetail();
		},
		methods: {
			returChName(value) {
				let name;
				let filterArr = this.$store.state.workProfession.filter(
					(item) => item.code == value
				);
				filterArr.length ? (name = filterArr[0].value) : (name = value);
				// options.filter(item=>item.code==value)[0].value

				return name;
			},
			// 作业完成，系统自动检查设备状态语音播报
			completeSpeech() {
				let text =
					"请停止作业，我将进行销点前复核，未检测到正常的动作曲线，请进行扳动试验。";
				initSpeech(text);
			},
			onlineMonit(param) {
				// 请求是否是道岔
				this.fetch(
					"POST",
					this.$store.state.baseUrl + CJX_DATA_CONTROLLER.IS_DCTYPE, {
						equType: param.equTypeId,
					}
				).then((res) => {
					console.log("是否是道岔", res);
					if (res.success) {
						let line = this.PmsWoinfor.woline;
						let equTypeId = param.equTypeId;
						let equId = param.equId;
						let searchInfo = {
							lineName: this.PmsWoinfor.woLineName,
							lineId: this.PmsWoinfor.woLine,
							majorName: this.returChName(this.PmsWoinfor.major),
							majorCode: this.PmsWoinfor.major,
							stationName: this.PmsWoinfor.locationName,
							stationCode: this.PmsWoinfor.location,
							equTypeName: param.equTypeName,
							equTypeId: param.equTypeId,
							equName: param.equName,
							equId: param.equId,
						};
						if (res.data) {
							// 道岔
							uni.navigateTo({
								url: "/pages/monitorCurve/index?type=order&isSwitch=true&params=" +
									JSON.stringify(searchInfo),
							});
						} else {
							// 非道岔
							uni.navigateTo({
								url: "/pages/monitorCurve/index?type=order&params=" +
									JSON.stringify(searchInfo),
							});
						}
					}
				});
				return;
			},
			async recordScanOrNfc(param) {
				await this.fetch(
					"POST",
					this.$store.state.baseUrl + WORK_CONTROLLER.Mark_Equ,
					param,
					false
				).then((res) => {
					if (res?.success) {
						uni.showToast({
							title: res.message,
							icon: "none",
						});
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
						});
					}
				});
			},
			returnEndCheck() {
				let status = false;
				if (this.columShow.nfc || this.columShow.scanCode) {
					return;
				}
			},

			getLocationUtil() {
				return new Promise((resolve, reject) => {
					uni.showToast({
						title: "获取位置信息中...",
						icon: "none",
					});
					// #ifdef APP-PLUS
					uni.getLocation({
						type: "gcj02",
						geocode: "true",
						success: function(res) {
							resolve({
								longitude: res.longitude,
								latitude: res.latitude,
							});
						},
						// 定位失败
						fail: function(error) {
							console.log(error);
							uni.showModal({
								title: "提示",
								content: error.errMsg,
							});
							reject(error);
						},
					});
					// #endif
				});
			},
			async getReturnLocation(param, param2) {
				await this.fetch(
						"Get",
						this.$store.state.baseUrl + WORK_CONTROLLER.Get_Equ_JudgeInRaw,
						param
					)
					.then(async (res) => {
						let CopyItem = this.copyTaskList[this.taskIndex];
						let findIndex = this.taskList.findIndex(
							(item) => item.equId == CopyItem.equId
						);
						if (
							res.success &&
							res.data.inRawLocation != "NO" &&
							res.data.have == "YES"
						) {
							if (param2 == "NFC") {
								await this.recordScanOrNfc({
									detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
									equId: this.copyTaskList[this.taskIndex].equId,
									nfcStatus: 2,
								});
								this.$set(this.copyTaskList[this.taskIndex], "nfcStatus", 2);
								this.$set(this.taskList[findIndex], "nfcStatus", 2);
							} else if (param2 == "扫码") {
								await this.recordScanOrNfc({
									detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
									equId: this.copyTaskList[this.taskIndex].equId,
									nfcStatus: 1,
								});
								this.$set(this.copyTaskList[this.taskIndex], "nfcStatus", 1);
								this.$set(this.taskList[findIndex], "scanCode", "nfcStatus", 1);
							}
						} else if (
							res.success &&
							res.data.inRawLocation == "NO" &&
							res.data.have == "YES"
						) {
							uni.showToast({
								title: "扫码失败，距离超出10米范围",
								icon: "none",
							});
						} else if (res.success && res.data.have == "NO") {
							// uni.showToast({
							//   title: "该标签不存在",
							//   icon: "none",
							// });
						} else {
							uni.showToast({
								title: res.message,
								icon: "none",
							});
						}
					})
					.catch((error) => {
						console.log(error);
					});
			},
			async markEquInterface(param2) {
				let CopyItem = this.copyTaskList[this.taskIndex];
				let findIndex = this.taskList.findIndex(
					(item) => item.equId == CopyItem.equId
				);
				if (param2 == "NFC") {
					await this.recordScanOrNfc({
						detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
						equId: this.copyTaskList[this.taskIndex].equId,
						nfcStatus: 2,
					});
					this.$set(this.copyTaskList[this.taskIndex], "nfcStatus", 2);
					this.$set(this.taskList[findIndex], "nfcStatus", 2);
				} else if (param2 == "扫码") {
					await this.recordScanOrNfc({
						detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
						equId: this.copyTaskList[this.taskIndex].equId,
						nfcStatus: 1,
					});
					this.$set(this.copyTaskList[this.taskIndex], "nfcStatus", 1);
					this.$set(this.taskList[findIndex], "scanCode", "nfcStatus", 1);
				}
			},
			deviceShow() {
				let showState = false;
				let woState = this.config.woState;
				let arr = ["进行中", "待提交"];
				arr.includes(woState) && this.btn == 2 ?
					(showState = true) :
					(showState = false);
				console.log(showState, "时间点击效果");
				return showState;
			},
			reduceArray(initialArr, attr) {
				let uniqueArr = initialArr.reduce((acc, obj) => {
					let existingObj = acc.find((item) => item[attr] === obj[attr]);
					if (!existingObj) {
						acc.push(obj);
					}
					return acc;
				}, []);
				return uniqueArr;
			},
			// 标记完成
			marEqu(param) {
				this.fetch(
					"POST",
					this.$store.state.baseUrl + WORK_CONTROLLER.Mark_Equ,
					param,
					false
				).then((res) => {
					if (res?.success) {
						this.initDetail();
					}
				});
			},
			async initDetail() {
				await this.fetch(
					"GET",
					this.$store.state.baseUrl + WORK_CONTROLLER.GET_WoSn_DETAIL, {
						woSn: this.config.woSn,
						findAllTaskFlag: this.btn == 4,
					},
					false
				).then((res) => {
					if (res.success && res.data) {
						this.pmsWoDetailed = res.data.PmsWoDetailed;
						this.PmsWoinfor = res.data.PmsWoinfor;
						this.tagList[1].count = 0;
						this.tagList[2].count = 0;
						this.splitDta();
					} else {
						console.error(res);
					}
				});
			},
			splitDta() {
				let task = this.pmsWoDetailed;
				this.taskList = [];
				task.forEach((item, index) => {
					let mapDetail = item.equDetailList.map((mapItem) => {
						return {
							...mapItem,
							woSn: this.config.woSn,
							detailWorkId: item.idK,
							equTypeId: item.equTypeID,
							equTypeName: item.equTypeName,
						};
					});
					this.taskList.push(...mapDetail);
				});
				this.taskList = this.reduceArray(this.taskList, "equId");
				this.taskList.sort((a, b) => {
					return (
						b.monitorStatus +
						b.controlStatus -
						(a.monitorStatus + a.controlStatus)
					);
				});
				this.createTagList();
			},
			createTagList() {
				this.taskList.forEach((item, index) => {
					this.tagList[index + 3] = {
						name: item.equName,
						id: item.equId,
						type: this.config.pmsEquControl && item.monitorStatus == 2 ?
							"warning" : this.config.pmsEquControl && item.controlStatus == 1 ?
							"error" : "primary",
					};
					this.tagList[0].count = this.taskList.length;
					if (item.monitorStatus == 2) {
						this.tagList[1].count += 1;
					}
					if (item.controlStatus == 1) {
						this.tagList[2].count += 1;
					}
				});
				this.clickTag(this.clickTagParam);
			},
			//开始检修|结束检修
			startAndEndByEqu(param, type) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				});
				let api;
				type == "start" ?
					(api = WORK_CONTROLLER.Start_ByEqu) :
					type == "end" ?
					(api = WORK_CONTROLLER.End_ByEqu) :
					(api = WORK_CONTROLLER.POST_Monitor_Recheck);
				return new Promise((resovle, reject) => {
					this.fetch("POST", this.$store.state.baseUrl + api, param, false)
						.then(async (res) => {
							if (res?.success) {
								await this.initDetail();
								uni.hideLoading();
							} else {
								uni.hideLoading();
								uni.showToast({
									title: res.message,
									icon: "none",
								});
							}
							resovle(res);
						})
						.catch((error) => {
							uni.hideLoading();
							reject(error);
						});
				});
			},
			// 普通设备
			commonEqui() {
				let status = false;
				if (
					!this.columShow.monitor &&
					!this.columShow.scanCode &&
					!this.columShow.nfc
				) {
					status = true;
				}
				return status;
			},
			changeCheckbox(value, index) {
				this.taskIndex = index;
				// let CopyItem = this.copyTaskList[index];
				// let CopyItem = this.copyTaskList[this.taskIndex];
				this.taskList[index].equStatus = value.value;
				// if (!CopyItem) return;
				// let findIndex = this.taskList.findIndex(
				// 	(item) => item.equId == CopyItem.equId
				// );
				// this.taskList[findIndex].checkend = value.value;
				this.marEqu({
					detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
					equId: this.copyTaskList[this.taskIndex].equId,
					equStatus: 1,
				});

				// if (value.value) {
				// 	getApp().globalData.overInfo[index] = {
				// 		...getApp().globalData.overInfo[index],
				// 		detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
				// 		equId: this.copyTaskList[this.taskIndex].equId,
				// 		equStatus: 1,
				// 		clickTagParam: this.clickTagParam,
				// 	}
				// } else {
				// 	getApp().globalData.overInfo[index] = {
				// 		...getApp().globalData.overInfo[index],
				// 		clickTagParam: this.clickTagParam,
				// 		equStatus: false
				// 	}
				// }
			},
			setDate(obj) {
				let str = `${obj.year}-${obj.month}-${obj.day} ${obj.hour}:${obj.minute}:${obj.second}`;
				let CopyItem = this.copyTaskList[this.taskIndex];
				let findIndex = this.taskList.findIndex(
					(item) => item.equId == CopyItem.equId
				);
				this.copyTaskList[this.taskIndex][this.date_type] = str;
				this.taskList[findIndex][this.date_type] = str;

				if (this.date_type == "startTime" || this.date_type == "endTime") {
					this.setEquTime(findIndex);
				}

				// getApp().globalData.overInfo[this.taskIndex] = {
				// 	...getApp().globalData.overInfo[this.taskIndex],
				// 	clickTagParam: this.clickTagParam,
				// 	startTime: this.copyTaskList[this.taskIndex]?.startTime,
				// 	endTime: this.copyTaskList[this.taskIndex]?.endTime
				// }
			},
			setEquTime(index) {
				if (
					this.taskList[index]["startTime"] &&
					this.taskList[index]["endTime"]
				) {
					let data = {
						equStartTime: this.taskList[index]["startTime"],
						equEndTime: this.taskList[index]["endTime"],
						detailWorkId: this.taskList[index].detailWorkId,
					};
					this.fetch(
						"post",
						this.$store.state.baseUrl + WORK_CONTROLLER.SET_EQU_TIME,
						data,
						false
					).then((res) => {
						console.log(res);
					});
				}
			},
			onCancel() {
				this.show_date = false;
			},
			checkStartTime(index) {
				this.taskIndex = index;
				this.date_type = "startTime";
				this.show_date = true;
			},
			checkEndTime(index) {
				this.taskIndex = index;
				this.date_type = "endTime";
				this.show_date = true;
			},
			// 复检
			recheck(index) {
				this.taskIndex = index;
				this.startAndEndByEqu({
					detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
					equId: this.copyTaskList[this.taskIndex].equId,
					woSn: this.copyTaskList[this.taskIndex].woSn,
				});
			},
			clickTag(param) {
				this.activeTag = param.id;
				this.clickTagParam = param;
				if (param.id == "all") {
					this.copyTaskList = this.taskList;
				} else if (param.id == "view") {
					this.copyTaskList = this.taskList.filter(
						(item) => item.monitorStatus == 2
					);
				} else if (param.id == "control") {
					this.copyTaskList = this.taskList.filter(
						(item) => item.controlStatus == 1
					);
				} else {
					this.copyTaskList = this.taskList.filter(
						(item) => item.equId == param.id
					);
				}
				let obj = getApp().globalData.overInfo;
				if (Object.keys(obj).length > 0) {
					for (let item of Object.keys(obj)) {
						if (obj[item]?.clickTagParam.id == param.id) {
							this.copyTaskList[item].equStatus = obj[item]?.equStatus ?
								true :
								false;
							this.copyTaskList[item].startTime = obj[item]?.startTime;
							this.copyTaskList[item].endTime = obj[item]?.endTime;
						}
					}
				}
			},
			// 监督、卡控设备 异常的点击显示信息
			async reviewInfo(param, index) {
				this.description = ``;
				let data = {
					detailWorkId: param.detailWorkId,
					equId: param.equId,
				};
				index == 1 ?
					(this.title = "监督项问题信息") :
					(this.title = "卡控报警信息");
				if (index == 1) {
					await this.fetch(
						"GET",
						this.$store.state.baseUrl + WORK_CONTROLLER.GET_Monitor,
						data,
						false
					).then((res) => {
						if (res?.returnCode == 2004) {
							res.data.forEach((item, index) => {
								this.description += index + 1 + ". " + item.subName + "<br>";
							});
						}
					});
				}
				if (index == 2) {
					await this.fetch(
						"GET",
						this.$store.state.baseUrl + WORK_CONTROLLER.GET_Risks,
						data,
						false
					).then((res) => {
						if (res?.returnCode == 2004) {
							res.data.forEach((item, index) => {
								this.description +=
									index +
									1 +
									". " +
									item.armContent +
									"  (" +
									item.armHappenTime +
									")" +
									"<br>";
							});
						}
					});
				}
				this.showInfo = true;
			},
			async testNFC(index1, index) {
				this.taskIndex = index1;
				//这里用异步获取读取到的NFC数据
				const nfcId = await NFC.listenNFCStatus();

				let equLabelCode = this.copyTaskList[index1].equLabelCode;
				console.log(equLabelCode)
				console.log(nfcId)
				console.log(this.copyTaskList[index1].equId)
				let _this = this;
				if (!nfcId) {
					uni.showToast({
						title: "NFC读取失败",
						icon: "none",
						duration: 5000,
					});
					return;
				} else if (!equLabelCode) {
					uni.showToast({
						title: "设备标签码不存在，请检查",
						icon: "none",
						duration: 5000,
					});
				} else if (equLabelCode != nfcId) {
					uni.showToast({
						title: "设备数据不匹配,设备ID:" + equLabelCode + ",NFC结果:" + nfcId,
						icon: "none",
						duration: 5000,
					});
				} else {
					uni.showToast({
						title: "NFC读取成功：" + nfcId,
						icon: "none",
					});
					// 对于NFC识别成功 且 与设备码一致的进行设置开始时间
					// 获取所有的设备，并将当前点击的设备作为最终设置时间的设备
					let allTaskListOfLabelCode = this.copyTaskList.map((item,ind) => ({
						...item,
						originIndex: ind
					})).filter(item => item.equLabelCode == equLabelCode && item.originIndex !== index);

					if(allTaskListOfLabelCode&& allTaskListOfLabelCode.length){
						// 循环进行操作
						for (let ind = 0; ind < allTaskListOfLabelCode.length; ind++) {
							const element = allTaskListOfLabelCode[ind];
							this.taskIndex = element.originIndex;
							await this.doNFCSuccess(this.taskIndex);
						}
					}
					// 执行当前选中的设备
					this.taskIndex = index1;
					await this.doNFCSuccess(this.taskIndex);
				}
			},
			async doNFCSuccess(taskIndex){
				let _this = this;
				this.start(taskIndex)
				let flag =
					parseInt(this.$store.state.appServer.appLocationFlag) == 1 ?
					true :
					false;
				if (flag) {
					_this.getLocationUtil().then((resLocal) => {
						let params = {
							longitude: resLocal.longitude,
							latitude: resLocal.latitude,
							equCode: nfcId,
						};
						_this.getReturnLocation(params, "NFC");
					});
				} else {
					
					_this.markEquInterface("NFC");
				}
			},
			back() {
				uni.navigateBack();
			},
			start(index1) {
				this.taskIndex = index1;
				this.startAndEndByEqu({
						detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
						equId: this.copyTaskList[this.taskIndex].equId,
						woSn: this.copyTaskList[this.taskIndex].woSn,
					},
					"start"
				);
			},
			// 增加
			async end(index1) {
				this.taskIndex = index1;
				this.startAndEndByEqu({
							detailWorkId: this.copyTaskList[this.taskIndex].detailWorkId,
							equId: this.copyTaskList[this.taskIndex].equId,
							woSn: this.copyTaskList[this.taskIndex].woSn,
						},
						"end"
					)
					.then((res) => {
						if (res?.success && this.$store.state.speechConfig) {
							initSpeech("设备正常，请尽快销点");
						}
					})
					.catch((error) => {});
			},
			unScanCode() {
				return new Promise((resolve, reject) => {
					uni.scanCode({
						success: function(res) {
							let equCode = res.result;
							uni.showToast({
								title: "设备扫码成功：" + res.result,
								icon: "none",
							});
							resolve(res.result);
						},
					});
				});
			},
			//  扫码
			async scanCode(index1, index) {
				this.taskIndex = index1;
				let _this = this;
				// 获取扫码的结果
				let scanCode = await this.unScanCode((res) => {
					return res;
				});
				// 获取选中设备的设备二维码
				let equLabelCode = this.copyTaskList[index1].equLabelCode;
				if (!scanCode) {
					uni.showToast({
						title: "扫码读取失败",
						icon: "none",
						duration: 5000,
					});
					return;
				} else if (!equLabelCode) {
					uni.showToast({
						title: "设备标签码不存在，请检查",
						icon: "none",
						duration: 5000,
					});
				} else if (equLabelCode != scanCode) {
					uni.showToast({
						title: "设备数据不匹配,设备ID:" + equLabelCode + ",扫码结果:" + scanCode,
						icon: "none",
						duration: 5000,
					});
				} else {
					// 对于扫码成功 且 与设备码一致的进行设置开始时间
					// 获取所有的设备，并将当前点击的设备作为最终设置时间的设备
					let allTaskListOfLabelCode = this.copyTaskList.map((item,ind) => ({
						...item,
						originIndex: ind
					})).filter(item => item.equLabelCode == equLabelCode && item.originIndex !== index);
					
					console.log(allTaskListOfLabelCode, "获取数据")
					if(allTaskListOfLabelCode&& allTaskListOfLabelCode.length){
						// 循环进行操作
						for (let ind = 0; ind < allTaskListOfLabelCode.length; ind++) {
							const element = allTaskListOfLabelCode[ind];
							this.taskIndex = element.originIndex;
							await this.doScanSuccess(this.taskIndex);
						}
					}
					// 执行当前选中的设备
					this.taskIndex = index1;
					await this.doScanSuccess(this.taskIndex);
					
				}
			},
			async doScanSuccess(taskIndex){
				let _this = this;
				this.start(taskIndex)
				let flag =
					parseInt(this.$store.state.appServer.appLocationFlag) == 1 ?
					true :
					false;
				if (flag) {
					// 启用gis位置定位
					let loPram = await this.getLocationUtil().then((resLocal) => {
						let params = {
							longitude: resLocal.longitude,
							latitude: resLocal.latitude,
							equCode: scanCode,
						};
						return params;
					});
					await this.getReturnLocation(loPram, "扫码");
				} else {
					_this.markEquInterface("扫码");
				}
			},
			formatDate(time) {
				var date = new Date(time);
				var year = date.getFullYear(),
					month = date.getMonth() + 1, //月份是从0开始的
					day = date.getDate(),
					hour = date.getHours(),
					min = date.getMinutes(),
					sec = date.getSeconds();
				var newTime =
					year +
					"-" +
					(month < 10 ? "0" + month : month) +
					"-" +
					(day < 10 ? "0" + day : day) +
					" " +
					(hour < 10 ? "0" + hour : hour) +
					":" +
					(min < 10 ? "0" + min : min) +
					":" +
					(sec < 10 ? "0" + sec : sec);

				return newTime;
			},
			saveDeviceList(obj) {},
		},
	};
</script>

<style scoped lang="less">
	.onlineMonit {
		padding: 5px;
		line-height: 20px;

		.u-btn {
			height: 30px;
			line-height: 30px;
		}
	}

	/deep/.u-mode-center-box {
		max-width: 90%;
		min-width: 200px;
	}

	.columsItem {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
	}

	.recheck {
		width: 50px;
		margin-top: 5px;
	}

	/deep/ .u-checkbox__icon-wrap--disabled--checked {
		.u-iconfont::before {
			color: #19be6b;
		}

		.u-checkbox__label {
			margin: 0px;
		}
	}

	.card_head {}

	.tagClass1 {
		background-color: #f6f6f6;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 10px 10px 0 10px;

		.tagClass_item {
			margin-right: 5px;
		}
	}

	.tagClass {
		padding: 5px 10px 5px;
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
		align-items: center;
		background-color: #f6f6f6;
		border-bottom: 1px solid #eaeaea;

		.tagClass_item {
			margin-right: 5px;
			margin-bottom: 5px;
		}
	}

	/deep/.u-model__footer {
		border-top: 1px solid #f0f0f0;
	}

	.u-icon {
		font-size: 16px;
	}

	.rowClass {
		display: flex;
		flex-direction: row;

		&:nth-child(2) {
			// padding-left: 5px!important;
		}
	}

	.lineClass {
		padding: 0 5px;
		// height: 40px;
	}

	.content {
		height: 100vh;
		width: 100%;
		padding-bottom: 0px;
	}

	/deep/.u-card__body {
		background-color: #fafafa;
	}

	.body_box_item {
		background-color: #fff;
		margin-bottom: 5px;
		border-radius: 10px;
		// margin: 0 10px 5px;
		padding: 0 10px;

		>.u-cell:nth-child(1) {
			font-weight: 600;
		}
	}

	.body_box {
		// background-color: #fafafa;
	}

	.u-cell {
		padding: 6px 10px !important;
	}

	.scroll-Y {
		flex: 1;
		height: 0;
		background-color: #f2f2f2;
		padding-top: 0px;
	}

	.footer {
		text-align: center;
		padding: 10px;
		background-color: #fff;
		margin-top: 5px;
		border-top: 1px solid #f6f6f6;
		box-shadow: 5px -1px 5px #f6f6f6;
	}

	/deep/.u-swipe-action {
		margin-bottom: 5px !important;
		border-radius: 5px;
	}

	/deep/.u-card__body {
		padding: 0px 0px 0px !important;
	}

	.line-select {
		height: calc(100vh - 132rpx);
		display: flex;
		flex-direction: column;

		.cordClass {
			padding: 0px 5px;
		}

		.card_head {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.title {
				font-weight: 600;
			}
		}
	}
</style>